package com.timothy.junk.gfund.activity;

/**
 * Created by qtang on 7/2/13.
 */
import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.markupartist.android.widget.PullToRefreshListView;
import com.sina.oefund.SinaOEFundClient;
import com.sina.stock.SinaStockClient;
import com.timothy.junk.R;
import com.timothy.junk.ActivityManage;
import com.timothy.junk.adapter.GFundAdaptor;
import com.timothy.junk.calculate.GFundCalculator;
import com.timothy.junk.common.parse.ParseXML;
import com.timothy.junk.common.util.NetWorkUtil;
import com.timothy.junk.gfund.bean.GFundBean;
import com.timothy.junk.gfund.bean.GFundBeanResult;

public class GFundList extends ListActivity {
	public static final String LOG_TAG = "GFundList";

    Context mContext;
    GFundAdaptor adaptor ;
    
//    boolean isIoadingComplete;
//    View mLoadingView;
    PullToRefreshListView mContentView;
    List<GFundBean> gFundList;

    SinaStockClient mClient;
    SinaOEFundClient fClient;
    
    ProgressBar progressBar;
    int mShortAnimationDuration = 200;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gfund_list);
        mContext = getApplicationContext();
        ActivityManage.add(this);
        
        mContentView = (PullToRefreshListView) getListView();
//        mContentView.setVisibility(View.GONE);
        
        progressBar = (ProgressBar) findViewById(R.id.loading_spinner);
        
        // Set a listener to be invoked when the list should be refreshed.
        mContentView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new GetDataTask().execute();
            }
        });

        if(NetWorkUtil.isNetworkAvailable(mContext)){
        	gFundList = ParseXML.getGFundList(mContext);
        	new GetDataTask().execute();
        }else{
        	Toast.makeText(mContext, "Your network is not available!", Toast.LENGTH_LONG).show();
        }
        
        mContentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(LOG_TAG,"-----onItemClick()-----");
				Intent intent=new Intent(mContext, GFundDetail.class);
				GFundBean sBean = gFundList.get(arg2-1);
				intent.putExtra("index",sBean.getIndex());
				startActivity(intent);
			}
        });
    }
    
    List<GFundBeanResult> gFundResultList;
    public void loadData(){
        GFundCalculator cal = new GFundCalculator(gFundList);
        cal.initialAllInputData();
        cal.getStockData(SinaStockClient.getInstance());
        cal.getFundData(SinaOEFundClient.getInstance());
        cal.calculateResult();
    	gFundResultList = cal.getResultDataList();
    }
    
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
                loadData();
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] result) {
            super.onPostExecute(result);
            if(gFundResultList!=null&&gFundResultList.size()>0){
//            	progressBar.setVisibility(View.INVISIBLE);
//              ((PullToRefreshListView) getListView()).setVisibility(View.VISIBLE);
            	
            	if(adaptor == null){
            		adaptor = new GFundAdaptor(mContext, gFundResultList);
            		setListAdapter(adaptor); 
            	}else{
            		adaptor.notifyDataSetChanged();
            	}
                
                mContentView.onRefreshComplete();
                progressBar.setVisibility(View.GONE);
                
                //start notify service
//                Intent intent = new Intent(mContext, NotifyService.class);
//                startService(intent);
            }else{
            	Toast.makeText(mContext, "Can not get any data!", Toast.LENGTH_LONG).show();
            }

        }
    }
    
 

}
